package com.itheima.anno;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
@WebServlet("/hg/userServlet")
public class UserServlet22 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收前端页面传递的参数信息
        String username = req.getParameter("username");
        String age = req.getParameter("age");
        String sex = req.getParameter("sex");
        //将信息以zhangsan,18,man 这种方式写出到项目根目录下的user.txt文件中
        ServletContext sc = getServletContext();
        String realPath = sc.getRealPath("/user.txt");
        //创建字符缓冲输出流将信息写入文件中
        BufferedWriter bw = new BufferedWriter(new FileWriter(realPath,true));
        bw.write(username+","+age+","+sex);
        bw.newLine();
        bw.close();
        //向页面添加一个输出成功
        resp.getWriter().write("succecc....");
    }
}
